﻿using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddEditCompany : System.Web.UI.Page
{
    CommonMethods cMthd = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getAllCompanies();
        }
    }
    [WebMethod]
    public static string[] SearchCities(string prefixText, int count)
    {
        using (POSEntities db = new POSEntities())
        {

            var cities = (from c in db.Cities where c.City_Name.Contains(prefixText) select c).ToArray();
            return cities.Select(c => AutoCompleteExtender.CreateAutoCompleteItem(c.City_Name, c.City_ID.ToString())).ToArray();
        }
    }
    protected void btnSaveCompany_Click(object sender, EventArgs e)
    {
        if (btnSaveCompany.Text == "Save")
        {
            using (POSEntities db = new POSEntities())
            {
                Company company = new Company();
                company.CompanyName = txtCompany.Text;
                company.Address = txtAddress.Text;
                company.City = Convert.ToInt32(hdfCity.Value.ToString());
                company.Phone = txtPhone.Text;
                db.Companies.Add(company);
                db.SaveChanges();
            }
        }
        else if (btnSaveCompany.Text == "Update")
        {
            int cId = Convert.ToInt32(btnSaveCompany.CommandArgument);
            using (POSEntities db = new POSEntities())
            {
                var company = (from c in db.Companies
                               where c.CompanyId == cId
                               select c).FirstOrDefault();
                company.CompanyName = txtCompany.Text;
                company.Address = txtAddress.Text;
                company.City = Convert.ToInt32(hdfCity.Value.ToString());
                company.Phone = txtPhone.Text;
                db.SaveChanges();
                btnSaveCompany.CommandArgument = "";
                btnSaveCompany.Text = "Save";
                getAllCompanies();
            }
        }
        clearForm();
    }
    private void getAllCompanies()
    {
        GridViewCompanies.DataSource = cMthd.getAllCompanies();
        GridViewCompanies.DataBind();
    }

    private void clearForm()
    {
        txtCompany.Text = "";
        txtAddress.Text = "";
        txtCity.Text = "";
        txtPhone.Text = "";
        hdfCity.Value = "";
        btnSaveCompany.CommandArgument = "";
        btnSaveCompany.Text = "Save";
    }

    protected void lbtnEditCompany_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var company = (from c in db.Companies
                           join cit in db.Cities on c.City equals cit.City_ID
                           where c.CompanyId == cId
                           select new { c.CompanyId, c.CompanyName, c.Phone, c.City, c.Address, cit.City_Name }).FirstOrDefault();
            txtCompany.Text = company.CompanyName;
            txtAddress.Text = company.Address;
            txtCity.Text = company.City_Name;
            txtPhone.Text = company.Phone;
            hdfCity.Value = company.City.ToString();
            btnSaveCompany.CommandArgument = company.CompanyId.ToString();
            btnSaveCompany.Text = "Update";

        }
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        clearForm();
    }
}